/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.apache.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import summary.android.activity.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * 
 * @author Sibaozuan(superskysbz@gmail.com)
 *
 */
public class HttpClientSummary extends Activity {
	private Button get;
	private Button login;
	private EditText response;
	private HttpClient mHttpClient;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.http_client_layout);
		mHttpClient = new DefaultHttpClient();
		get = (Button) findViewById(R.id.get);
		login = (Button) findViewById(R.id.login);
		response = (EditText) findViewById(R.id.response);
		get.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HttpGet get = new HttpGet("http://192.168.0.147/summary/");
				try {
					HttpResponse mHttpResponse = mHttpClient.execute(get);
					HttpEntity entity = mHttpResponse.getEntity();
					if (entity != null) {
						BufferedReader mBufferedReader = new BufferedReader(
								new InputStreamReader(entity.getContent()));
						String line = null;
						while ((line = mBufferedReader.readLine()) != null) {
							response.append(line + "\n");
						}
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final View loginDialog = getLayoutInflater().inflate(R.layout.login_layout,
						null);
				new AlertDialog.Builder(getApplicationContext()).setTitle("登录系统")
						.setView(loginDialog)
						.setPositiveButton("登录", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								String name = ((EditText) loginDialog
										.findViewById(R.id.name)).getText()
										.toString();

								String pwd = ((EditText) loginDialog
										.findViewById(R.id.pwd)).getText()
										.toString();
								HttpPost mHttpPost = new HttpPost(
										"http://192.168.0.147/summary/");
								List<NameValuePair> params = new ArrayList<NameValuePair>();
								params.add(new BasicNameValuePair("name", name));
								params.add(new BasicNameValuePair("pwd", pwd));
								try {
									mHttpPost.setEntity(new UrlEncodedFormEntity(
											params, HTTP.UTF_8));
									HttpResponse mHttpResponse = mHttpClient
											.execute(mHttpPost);
									if (mHttpResponse.getStatusLine()
											.getStatusCode() == 200) {
										String msg = EntityUtils
												.toString(mHttpResponse
														.getEntity());
										Toast.makeText(getApplicationContext(),
												msg, 5000).show();
									}
								} catch (UnsupportedEncodingException e) {
									e.printStackTrace();
								} catch (ClientProtocolException e) {
									e.printStackTrace();
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}).setNegativeButton("取消", null).show();
			}
		});
	}
}
